package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.CustomerMemberDiscontent;
import com.lebang.example.CustomerFeedbackCategoryExample;
import com.lebang.example.CustomerMemberDiscontentExample;
import com.lebang.service.CustomerFeedbackCategoryService;
import com.lebang.service.CustomerMemberDiscontentService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;


/**
 * upload_img_record
 *
 * @author hwq
 * @date 2020-07-23
 */
@Controller
@RequestMapping("/sys/customermemberdiscontent")
public class CustomerMemberDiscontentController {
    @Autowired
    private CustomerMemberDiscontentService baseService;
    @Autowired
    private CustomerFeedbackCategoryService customerFeedbackCategoryService;

//    @Login
    @GetMapping("/list")
    public String list(Model model) {
        //获取反馈类目
        CustomerFeedbackCategoryExample FeedbackNameExample=new CustomerFeedbackCategoryExample();
        model.addAttribute("feedbackName",customerFeedbackCategoryService.selectByExample(FeedbackNameExample));
        return "/customermemberdiscontent/list";
    }

    @Login
    @GetMapping("/add")
    public String add(Model model,
                      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime star,
                      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,
                      String appStore) {
        model.addAttribute("item", baseService.selectByStatistics(star, end, appStore));
        return "/customermemberdiscontent/edit";
    }


    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(CustomerMemberDiscontent vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Long id) {
        baseService.deleteById(id);
        return Callback.success("success", "操作成功");
    }

    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request,
                           @RequestParam Long page,
                           @RequestParam Integer limit,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime star,
                           @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,
                           String appStore,String feedbackName) {
        CustomerMemberDiscontentExample discontentExample=new CustomerMemberDiscontentExample();
        CustomerMemberDiscontentExample.Criteria criteria = discontentExample.createCriteria();
        discontentExample.setOffset((page - 1) * limit);
        discontentExample.setLimit(limit);
        if (!TextUtil.isEmpty(star, end)) {
            criteria.andCreateTimeBetween(star, end);
        }

        if (!TextUtil.isEmpty(appStore)){
            criteria.andAppStoreEqualTo(appStore);
        }
        if (!TextUtil.isEmpty(feedbackName)){
            criteria.andFeedbackCategoryEqualTo(feedbackName);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(discontentExample));
        jsonObject.put("count", baseService.countByExample(discontentExample));
        return jsonObject;
    }
}
